'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  // 首页
  async index() {
    const { ctx } = this;

    if (!ctx.session.visited) {
      ctx.session.visited = 1;
    } else {
      ctx.session.visited++;
    }
    await ctx.render('index', {
      title: '微博首页',
      blogList: [
        { id: 1, title: 'aaa' },
        { id: 2, title: 'bbb' },
        { id: 3, title: 'ccc' },
      ],
      visited: ctx.session.visited,
    });
  }

  // 个人主页
  async profilePage() {
    // 获取参数
    const { userName } = this.ctx.params;
    this.ctx.body = {
      title: 'this is profile page!',
      userName,
    };
  }

  // 加载更多API
  async loadMore() {
    const { userName, pageIndex } = this.ctx.params;
    this.ctx.body = {
      title: 'this is loadMore API',
      userName,
      pageIndex,
    };
  }

  // 404页面
  async notFound() {
    await this.ctx.render('404');
  }
}

module.exports = HomeController;
